WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
buffer.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// Wolf includes
22#include "core/common/wolf.h"
23
24namespace wolf
25{
30template <typename T>
31class Buffer
32{
33 public:
34 typedef typename std::map<TimeStamp, T>::iterator Iterator;
35 typedef typename std::map<TimeStamp, T>::const_iterator ConstIterator;
36
39 Buffer(){};
40
43 ~Buffer(void){};
44
50 T select(const TimeStamp& _time_stamp, const double& _time_tolerance) const;
51
57 ConstIterator selectIterator(const TimeStamp& _time_stamp, const double& _time_tolerance) const;
58
61 Iterator selectIterator(const TimeStamp& _time_stamp, const double& _time_tolerance);
62
65 T selectFirstBefore(const TimeStamp& _time_stamp, const double& _time_tolerance) const;
66
69 T selectLastAfter(const TimeStamp& _time_stamp, const double& _time_tolerance) const;
70
73 T selectFirst() const;
74
77 T selectLast() const;
78
81 SizeStd size(void) const;
82
85 void emplace(const TimeStamp& _time_stamp, const T& _element);
86
91 const std::map<TimeStamp, T>& getContainer() const;
92
95 std::map<TimeStamp, T>& getContainer();
96
99 void removeUpTo(const TimeStamp& _time_stamp);
100
103 void removeUpToLower(const TimeStamp& _time_stamp);
104
107 void clear();
108
111 bool empty() const;
112
116 static bool checkTimeTolerance(const TimeStamp& _time_stamp1,
117 const TimeStamp& _time_stamp2,
118 const double& _time_tolerance);
119
123 static bool doubleCheckTimeTolerance(const TimeStamp& _time_stamp1,
124 const double& _time_tolerance1,
125 const TimeStamp& _time_stamp2,
126 const double& _time_tolerance2);
127
128 protected:
129 std::map<TimeStamp, T> container_;
130};
131
132} // namespace wolf
133
135
136namespace wolf
137{
138template <typename T>
140 const double& _time_tolerance) const
141{
142 assert(_time_stamp.ok() && "Buffer: invalid timestamp!");
143 Buffer<T>::ConstIterator post = container_.upper_bound(_time_stamp);
144
145 bool prev_exists = (post != container_.begin());
146 bool post_exists = (post != container_.end());
147
148 bool post_ok = post_exists && checkTimeTolerance(post->first, _time_stamp, _time_tolerance);
149
150 if (prev_exists)
151 {
152 Buffer<T>::ConstIterator prev = std::prev(post);
153
154 bool prev_ok = checkTimeTolerance(prev->first, _time_stamp, _time_tolerance);
155
156 if (prev_ok && !post_ok)
157 return prev;
158
159 else if (!prev_ok && post_ok)
160 return post;
161
162 else if (prev_ok && post_ok)
163 {
164 if (std::fabs(post->first - _time_stamp) < std::fabs(prev->first - _time_stamp))
165 return post;
166 else
167 return prev;
168 }
169 }
170 else if (post_ok)
171 return post;
172
173 return container_.end();
174}
175
176template <typename T>
177inline typename Buffer<T>::Iterator Buffer<T>::selectIterator(const TimeStamp& _time_stamp,
178 const double& _time_tolerance)
179{
180 assert(_time_stamp.ok() && "Buffer: invalid timestamp!");
181 Buffer<T>::Iterator post = container_.upper_bound(_time_stamp);
182
183 bool prev_exists = (post != container_.begin());
184 bool post_exists = (post != container_.end());
185
186 bool post_ok = post_exists && checkTimeTolerance(post->first, _time_stamp, _time_tolerance);
187
188 if (prev_exists)
189 {
190 Buffer<T>::Iterator prev = std::prev(post);
191
192 bool prev_ok = checkTimeTolerance(prev->first, _time_stamp, _time_tolerance);
193
194 if (prev_ok && !post_ok)
195 return prev;
196
197 else if (!prev_ok && post_ok)
198 return post;
199
200 else if (prev_ok && post_ok)
201 {
202 if (std::fabs(post->first - _time_stamp) < std::fabs(prev->first - _time_stamp))
203 return post;
204 else
205 return prev;
206 }
207 }
208 else if (post_ok)
209 return post;
210
211 return container_.end();
212}
213
214template <typename T>
215inline T Buffer<T>::select(const TimeStamp& _time_stamp, const double& _time_tolerance) const
216{
217 assert(_time_stamp.ok() && "Buffer: invalid timestamp!");
218 if (container_.empty()) return T(); // nullprt in case of T being a pointer
219
220 auto it = selectIterator(_time_stamp, _time_tolerance);
221
222 // end is returned from selectIterator if an element of the buffer complying with the time stamp
223 // and time tolerance has not been found
224 if (it != container_.end())
225 {
226 return it->second;
227 }
228
229 return T(); // nullprt in case of T being a pointer
230}
231
232template <typename T>
233inline T Buffer<T>::selectFirstBefore(const TimeStamp& _time_stamp, const double& _time_tolerance) const
234{
235 assert(_time_stamp.ok() && "Buffer: invalid timestamp!");
236 // There is no element
237 if (container_.empty()) return T(); // nullprt in case of T being a pointer
238
239 // Checking on begin() since elements are ordered in time
240 // Return first element if is older than time stamp
241 if (container_.begin()->first < _time_stamp) return container_.begin()->second;
242
243 // Return first element if despite being newer, it is within the time tolerance
244 if (checkTimeTolerance(container_.begin()->first, _time_stamp, _time_tolerance)) return container_.begin()->second;
245
246 // otherwise return nullptr (no element before the provided ts or within the tolerance was found)
247 return T(); // nullprt in case of T being a pointer
248}
249
250template <typename T>
251inline T Buffer<T>::selectLastAfter(const TimeStamp& _time_stamp, const double& _time_tolerance) const
252{
253 assert(_time_stamp.ok() && "Buffer: invalid timestamp!");
254 // There is no element
255 if (container_.empty()) return T(); // nullprt in case of T being a pointer
256
257 // Checking on rbegin() since elements are ordered in time
258 // Return last element if is newer than time stamp
259 if (container_.rbegin()->first > _time_stamp) return container_.rbegin()->second;
260
261 // Return last element if despite being older, it is within the time tolerance
262 if (checkTimeTolerance(container_.rbegin()->first, _time_stamp, _time_tolerance))
263 return container_.rbegin()->second;
264
265 // otherwise return nullptr (no element after the provided ts or within the tolerance was found)
266 return T(); // nullprt in case of T being a pointer
267}
268
269template <typename T>
271{
272 // There is no element
273 if (container_.empty()) return T(); // nullprt in case of T being a pointer
274
275 // Returning first map element
276 return container_.begin()->second;
277}
278
279template <typename T>
280inline T Buffer<T>::selectLast() const
281{
282 // There is no element
283 if (container_.empty()) return T(); // nullprt in case of T being a pointer
284
285 // Returning last map element
286 return container_.rbegin()->second;
287}
288
289template <typename T>
290inline void Buffer<T>::emplace(const TimeStamp& _time_stamp, const T& _element)
291{
292 assert(_time_stamp.ok() && "Buffer: invalid timestamp!");
293 container_.emplace(_time_stamp, _element);
294}
295
296template <typename T>
297inline const std::map<TimeStamp, T>& Buffer<T>::getContainer() const
298{
299 return container_;
300}
301
302template <typename T>
303inline std::map<TimeStamp, T>& Buffer<T>::getContainer()
304{
305 return container_;
306}
307
308template <typename T>
309inline void Buffer<T>::clear()
310{
311 container_.clear();
312}
313
314template <typename T>
315inline bool Buffer<T>::empty() const
316{
317 return container_.empty();
318}
319
320template <typename T>
321inline SizeStd Buffer<T>::size(void) const
322{
323 return container_.size();
324}
325
326template <typename T>
327inline void Buffer<T>::removeUpTo(const TimeStamp& _time_stamp)
328{
329 assert(_time_stamp.ok() && "Buffer: invalid timestamp!");
330 Buffer::Iterator post = container_.upper_bound(_time_stamp);
331 container_.erase(container_.begin(), post); // erasing by range
332}
333
334template <typename T>
335inline void Buffer<T>::removeUpToLower(const TimeStamp& _time_stamp)
336{
337 assert(_time_stamp.ok() && "Buffer: invalid timestamp!");
338 Buffer::Iterator post = container_.lower_bound(_time_stamp);
339 container_.erase(container_.begin(), post); // erasing by range
340}
341
342template <typename T>
343inline bool Buffer<T>::doubleCheckTimeTolerance(const TimeStamp& _time_stamp1,
344 const double& _time_tolerance1,
345 const TimeStamp& _time_stamp2,
346 const double& _time_tolerance2)
347{
348 double time_diff = std::fabs(_time_stamp1 - _time_stamp2);
349 double time_tol = std::min(_time_tolerance1, _time_tolerance2);
350 bool pass = time_diff <= time_tol;
351 return pass;
352}
353
354template <typename T>
355inline bool Buffer<T>::checkTimeTolerance(const TimeStamp& _time_stamp1,
356 const TimeStamp& _time_stamp2,
357 const double& _time_tolerance)
358{
359 double time_diff = std::fabs(_time_stamp1 - _time_stamp2);
360 bool pass = time_diff <= _time_tolerance;
361 return pass;
362}
363
364// SPECIFIC BUFFERS
365// fwd declarations
366class VectorComposite;
367
370class BufferFrame : public Buffer<FrameBasePtr>
371{
372 public:
375 void emplace(FrameBasePtr _frm);
376};
377
380class BufferCapture : public Buffer<CaptureBasePtr>
381{
382 public:
385 void emplace(CaptureBasePtr _cap);
386};
387
390class BufferVectorComposite : public Buffer<VectorComposite>
391{
392};
393
394} // namespace wolf
Buffer of Captures.
Definition: buffer.h:381
void emplace(CaptureBasePtr _cap)
Adds a capture to the buffer, with its time stamp.
Definition: buffer.cpp:30
Buffer of Frames.
Definition: buffer.h:371
void emplace(FrameBasePtr _frm)
Adds a frame to the buffer, with its time stamp.
Definition: buffer.cpp:25
Buffer of VectorComposite.
Definition: buffer.h:391
Buffer for arbitrary type objects.
Definition: buffer.h:32
static bool doubleCheckTimeTolerance(const TimeStamp &_time_stamp1, const double &_time_tolerance1, const TimeStamp &_time_stamp2, const double &_time_tolerance2)
Returns whether the time distance between two time stamps is smaller than or equal to the minimum of ...
Definition: buffer.h:343
SizeStd size(void) const
Returns the number of elements of the buffer.
Definition: buffer.h:321
std::map< TimeStamp, T >::iterator Iterator
Buffer iterator.
Definition: buffer.h:34
void clear()
Clears the buffer.
Definition: buffer.h:309
const std::map< TimeStamp, T > & getContainer() const
Returns the container with elements of the buffer.
Definition: buffer.h:297
void removeUpTo(const TimeStamp &_time_stamp)
Removes all elements in the buffer with a time stamp older than or equal to the specified.
Definition: buffer.h:327
std::map< TimeStamp, T >::const_iterator ConstIterator
Buffer const iterator.
Definition: buffer.h:35
void emplace(const TimeStamp &_time_stamp, const T &_element)
Adds an element to the buffer.
Definition: buffer.h:290
Iterator selectIterator(const TimeStamp &_time_stamp, const double &_time_tolerance)
Select an element iterator from the buffer (see the const version)
Definition: buffer.h:177
T select(const TimeStamp &_time_stamp, const double &_time_tolerance) const
Select an element from the buffer.
Definition: buffer.h:215
T selectFirst() const
Returns the oldest element (T() if empty)
Definition: buffer.h:270
Buffer()
Default constructor.
Definition: buffer.h:39
std::map< TimeStamp, T > & getContainer()
Returns the container with elements of the buffer.
Definition: buffer.h:303
T selectLast() const
Returns the newest element (T() if empty)
Definition: buffer.h:280
static bool checkTimeTolerance(const TimeStamp &_time_stamp1, const TimeStamp &_time_stamp2, const double &_time_tolerance)
Returns whether the time distance between two time stamps is smaller than or equal to the time tolera...
Definition: buffer.h:355
T selectLastAfter(const TimeStamp &_time_stamp, const double &_time_tolerance) const
Returns the newest element if it is newer than _time_stamp or within the time tolerance (T() otherwis...
Definition: buffer.h:251
~Buffer(void)
Destructor.
Definition: buffer.h:43
ConstIterator selectIterator(const TimeStamp &_time_stamp, const double &_time_tolerance) const
Select an element iterator from the buffer.
Definition: buffer.h:139
void removeUpToLower(const TimeStamp &_time_stamp)
Removes all elements in the buffer with a time stamp older than the specified.
Definition: buffer.h:335
bool empty() const
Returns whether the buffer is empty.
Definition: buffer.h:315
T selectFirstBefore(const TimeStamp &_time_stamp, const double &_time_tolerance) const
Returns the oldest element if it is older than _time_stamp or within the time tolerance (T() otherwis...
Definition: buffer.h:233
std::map< TimeStamp, T > container_
Main buffer container, ordered by time stamp.
Definition: buffer.h:129
TimeStamp implements basic functionalities for time stamps.
Definition: time_stamp.h:39
bool ok() const
Returns whether the time stamp has a valid value.
Definition: time_stamp.h:214
Composite of state vectors.
Definition: vector_composite.h:29
Definition: capture_base.h:23
std::size_t SizeStd
Other containers size type for the Wolf project.
Definition: wolf.h:66